<template>
    <div>
        <div class="nav">
            <img src="../assets/logo.png" alt="">
            <span class="title">XXX电商后台管理系统</span>
            <el-button type="info" plain @click="logout">退出登录</el-button>
        </div>
        <el-container>
            <el-aside :width="isCollapse ? '65px' : '300px'">
                <div class="toggleCollapse" @click="toggleCollapse">|||</div>
                <el-menu background-color="#545c64" active-text-color="#ffd04b" text-color="#fff" :collapse="isCollapse"  :router="true">
                <!--一级菜单-->
                <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
                    <template slot="title">
                        <i class="el-icon-location"></i>
                        <span>{{item.authName}}</span>
                    </template>
                    <!--二级菜单-->
                    <el-menu-item-group v-for="subItem in item.children" :key="subItem.id">
                        <el-menu-item :index="subItem.path">{{subItem.authName}}</el-menu-item>
                    </el-menu-item-group>
                    </el-submenu>
                </el-menu>
            </el-aside>
            <el-main>
                <router-view></router-view>
            </el-main>
        </el-container>
    </div>
</template>

<script>
export default {
    data () {
        return {
            menuList: [],
            menuIcon: {

            },
            subMenuIcon: {
            },
            isCollapse: false
        }
    },
    created () {
        this.getMenulist()
    },
    methods: {
        logout () {
            // 退出登录 销毁本地token
            window.sessionStorage.clear()
            this.$router.push('/login')
        },
        // 获取菜单列表
        async getMenulist () {
            const { data: res } = await this.$http.get('menus')
            if (res.meta.status !== 200) {
                return
            }
            this.menuList = res.data
        },
        // 切换侧边栏显示
        toggleCollapse () {
            this.isCollapse = !this.isCollapse
        }
    }
}
</script>

<style lang="less" scoped>
  .toggleCollapse {
      background-color: #333;
      color: white;
      line-height: 28px;
      font-size: 16px;
      text-align: center;
      cursor: pointer;
  }
  .nav img {
      height: 50px;
      width: 50px;
      margin-left: 10px;
  }
  .title {
      display: block;
      height: 40px;
      padding: 20px 0 20px 0;
  }
  .nav{
      display: flex;
      justify-content: space-between;
      height: 80px;
      background: #aaaaaa;
      font-size: 28px;
      font-weight: 500;
      align-items: center;
  }
  .el-main {
    background-color: #E9EEF3;
    color: #333;
    text-align: center;
    line-height: 820px;
    width: 150px;
  }
  body > .el-container {
    margin-bottom: 40px;
  }
  .el-aside {
    background-color: rgb(84, 92, 100);
    color: #333;
    text-align: center;
    line-height: 800px;
    height: 860px;
  }
  .el-submenu {
      text-align: left;
      border-right: 1px;
      span {
          font-size: 20px;
          cursor: pointer;
      }
  }
  .el-menu {
      border: none;
  }
</style>
